package com.shanghaionstar.weight;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.DeviceUtil;
import com.onstar.cn.weight.CustomImageView;
import com.shanghaionstar.R;

/**
 * Created by 7828wc on 16/6/28.
 */
public class TopLayout extends LinearLayout {
    private TextView top_title;
    private CustomImageView btn_back;
    private View view;
    private ImageButton right_btn;
    private int textColor;
    private TextView right_btn_text;




    private TextView close;
    private boolean isShowClose = false;

    public TextView getClose() {
        return close;
    }

    public void setClose(TextView close) {
        this.close = close;
    }

    /**
     * 增加可点击区域
     */
    private RelativeLayout frame_back;

    public TopLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(attrs);
    }

    public TopLayout(Context context) {
        super(context);
        initView(null);
    }

    public String getTitle() {
        return top_title.getText().toString();
    }

    /**
     * @return void
     * @Title: initView
     * @Description: 初始化View
     * @throws?
     */
    private void initView(AttributeSet attrs) {
        view = inflate(getContext(), R.layout.top_bar_layout, this);
        top_title = (TextView) view.findViewById(R.id.top_title);
        btn_back = (CustomImageView) view.findViewById(R.id.btn_back);
        right_btn = (ImageButton) view.findViewById(R.id.right_btn);
        right_btn_text = (TextView) view.findViewById(R.id.right_btn_text);
        frame_back = (RelativeLayout) view.findViewById(R.id.frame_back);
        close = (TextView) view.findViewById(R.id.close);

        if (attrs != null) {
            // 对应ATTR
            TypedArray typeArray = getContext().obtainStyledAttributes(attrs, com.onstar.cn.weight.R.styleable.input);
            String title = typeArray.getString(com.onstar.cn.weight.R.styleable.input_topTitle);
            boolean isShowBack = typeArray.getBoolean(com.onstar.cn.weight.R.styleable.input_isShowBack, true);
            boolean isShowHome = typeArray.getBoolean(com.onstar.cn.weight.R.styleable.input_isShowHome, false);
            isShowClose = typeArray.getBoolean(com.onstar.cn.weight.R.styleable.input_isShowColse, false);

            textColor = typeArray.getInteger(com.onstar.cn.weight.R.styleable.input_textColor, getResources().getColor(com.onstar.cn.weight.R.color.fontColor));
            if (isShowBack) {
                btn_back.setVisibility(View.VISIBLE);
            } else {
                btn_back.setVisibility(View.GONE);
            }
            if (isShowHome) {
                right_btn.setVisibility(View.VISIBLE);
            } else {
                right_btn.setVisibility(View.GONE);
            }
            if (!CommonUtil.isNull(title)) {
                top_title.setText(title);
            }
            if (isShowClose) {
                close.setVisibility(View.VISIBLE);
            } else {
                close.setVisibility(View.GONE);
            }

            btn_back.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (getContext() instanceof Activity) {
                        Activity activity = (Activity) getContext();
                        activity.setResult(Activity.RESULT_CANCELED);
                        ((Activity) getContext()).finish();
                    }
                }
            });
            frame_back.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (getContext() instanceof Activity) {
                        Activity activity = (Activity) getContext();
                        activity.setResult(Activity.RESULT_CANCELED);
                        ((Activity) getContext()).finish();
                    }
                }
            });

            // 释放
            typeArray.recycle();
        }
    }

    public void setTopTitle(String title) {
        if (!CommonUtil.isNull((title))) {
            top_title.setText(title);
        }
    }

    public void setTopTitle(int resId) {
        setTopTitle(getResources().getString(resId));
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        btn_back.setOnClickListener(onClickListener);
        frame_back.setOnClickListener(onClickListener);
    }

    public void setOnRightClickListener(String title, OnClickListener onClickListener) {
        right_btn.setOnClickListener(onClickListener);
        right_btn_text.setText(title);
        if (DeviceUtil.getLanguage().equalsIgnoreCase("en")) {
            right_btn_text.setTextSize(7);
        } else {
            right_btn_text.setTextSize(14);
        }
        right_btn_text.setTextColor(textColor);
        right_btn.setVisibility(View.VISIBLE);
    }

    public void showRightButton(boolean isShow) {
        right_btn.setVisibility(isShow ? View.VISIBLE : View.GONE);
        right_btn_text.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    public void setRightBtnRes(int resId) {
        right_btn.setImageResource(resId);
    }

    public void setRightTextBg(int resId) {
        right_btn_text.setBackgroundResource(resId);
    }

    public void setOnRightTextClickLstener(String title, OnClickListener onClickListener) {
        right_btn_text.setText(title);
        right_btn_text.setOnClickListener(onClickListener);
//        if (DeviceUtil.getLanguage().equalsIgnoreCase("en")) {
//            right_btn_text.setTextSize(7);
//        } else {
//            right_btn_text.setTextSize(14);
//        }
        right_btn_text.setTextSize(14);
        right_btn_text.setTextColor(textColor);
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    private View.OnClickListener closeClickListener;

    public void setCloseSetListener(View.OnClickListener clickListener) {
        this.closeClickListener = clickListener;
        close.setOnClickListener(closeClickListener);
    }

    public void showClose(boolean isShowClose) {
        int visible = isShowClose ? View.VISIBLE : View.GONE;
        close.setVisibility(visible);
        if (closeClickListener == null) {
            close.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (getContext() instanceof Activity) {
                        Activity activity = (Activity) getContext();
                        activity.finish();
                    } else {
                        close.setOnClickListener(closeClickListener);
                    }
                }
            });
        } else {
            close.setOnClickListener(closeClickListener);
        }
    }
}
